package taskoauthproject;

import java.io.IOException;
import java.util.Date;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.api.services.calendar.model.Events;
//import com.google.api.client.googleapis.auth.clientlogin.ClientLogin;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import com.google.api.services.calendar.Calendar.Events.List;

@SuppressWarnings("serial")

public class CreateEventServlet extends HttpServlet {
	
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		
		
		
	    req.getRequestDispatcher("calendar.jsp").forward(req, resp);

	}


	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		
//		String userId = 
		
		
		Calendar client = Utils.loadCalendarClient();

		
		
		Calendar.CalendarList.List listRequest = client.calendarList().list();

		listRequest.setFields("items(id,summary)");


		String calendarId = req.getParameter("calendarId");
		
		String description = req.getParameter("description");
		String summary = req.getParameter("summary");
		
		Event event = new Event();
		event.setDescription(description);
		event.setSummary(summary);
		
		
		Date startDate = new Date();
		Date endDate = new Date(startDate.getTime() + 3600000);
		DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
		event.setStart(new EventDateTime().setDateTime(start));
		DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
		event.setEnd(new EventDateTime().setDateTime(end));
		
		
		String id = client.events().insert(calendarId, event).execute().getId();
		
		
		
		resp.sendRedirect("calendar.jsp");
	}
}